using System;
using System.Xml.Serialization;

namespace WeatherRouter.Model
{
	/// <summary>
	/// Summary description for Feed.
	/// </summary>    
	public class Feed
	{
        public const int STANDARD_SYNC_HOUR = 8;
        public const int STANDARD_SYNC_MIN = 0;

        private string _name;
        private Guid _guid;
        private string _url;        
        private DateTime _lastSynchronized = DateTime.Now;
        private DateTime _nextSync;
        private int _syncHour = STANDARD_SYNC_HOUR;        
        private int _syncMin = STANDARD_SYNC_MIN;
        private string _schema;
        private string _itemSchema;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public Guid Guid
        {
            get { return _guid; }
            set { _guid = value; }
        }

        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        public DateTime LastSynchronized
        {
            get { return _lastSynchronized; }
            set { _lastSynchronized = value; }
        }

        public int SyncHour
        {
            get { return _syncHour; }
            set { _syncHour = value; }
        }

        public int SyncMin
        {
            get { return _syncMin; }
            set { _syncMin = value; }
        }

        [XmlIgnore]
        public DateTime NextSync
        {
            get
            {
                _nextSync = _lastSynchronized.Date;
                _nextSync = _nextSync.AddHours(_syncHour);
                _nextSync = _nextSync.AddMinutes(_syncMin);
                  
                // If the specified time already passed, add a day                 
                if (_nextSync < DateTime.Now)
                {
                    _nextSync = _nextSync.AddDays(1);
                }
                    
                return _nextSync;                
            }
            set { _nextSync = value; }
        }

        /// <summary>
        /// Schema used to convert xHtml to canonical xml
        /// </summary>
        public string Schema
        {
            get { return _schema; }
            set { _schema = value; }
        }

        /// <summary>
        /// Schema used to prepare the packet
        /// </summary>
        public string ItemSchema
        {
            get { return _itemSchema; }
            set { _itemSchema = value; }
        }

        public Feed()
        {
            _guid = Guid.NewGuid();
        }

        public Feed(Guid guid)
        {
            _guid = guid;
        }
    }
}
